<?

$running_upgrade_script = true;
include_once("includes/inc.global.php");

if(!DOWN_FOR_MAINTENANCE)
	die("You need to take the system down for maintenance before running the upgrade script.  See UPGRADE.txt for more information.");

$output = "<i><b>Upgrading Local Exchange to version ". LOCALX_VERSION ."...</b><p>";

$query = $cDB->Query("desc categories category_code");
if($row = mysql_fetch_array($query)) {
	if(strpos($row[1], "char") > 0) {
		die("Sorry, but this script does not support upgrades from version 0.3.0, which you appear to have.  It is recommended that you back up your data and do a fresh install.  Please note that you will need to recreate your categories and update the listings and trades tables to reflect the new categories.");
	}
}

$query = $cDB->Query("desc member join_fee_nc");
if(mysql_fetch_array($query)) {
	$cDB->Query("alter table member drop join_fee_nc") or die("Error altering member table.  Does the web user account have alter table permission?");
	$cDB->Query("alter table member drop join_fee_lc") or die("Error altering member table.  Does the web user account have alter table permission?");
	$cDB->Query("alter table member add email_updates int(3) unsigned NOT NULL default '7'") or die("Error altering member table.  Does the web user account have alter table permission?");
	$output .= "Altered member table<br>";	
}

$query = $cDB->Query("desc trades description");
if($row = mysql_fetch_array($query)) {
	if(strpos($row[1], "150") > 0) {
		$cDB->Query("alter table trades modify description varchar(255) default NULL") or die("Error altering trades table.  Does the web user account have alter table permission?");
		$output .= "Altered trades table<br>";	
	}
}

$query = $cDB->Query("desc feedback comment");
if($row = mysql_fetch_array($query)) {
	if(strpos($row[1], "255") > 0) {
		$cDB->Query("alter table feedback modify comment text") or die("Error altering feedback table.  Does the web user account have alter table permission?");
		$output .= "Altered feedback table<br>";	
	}
}

$query = $cDB->Query("desc uploads");
	if(! $query) {
		$cDB->Query("CREATE TABLE uploads ( upload_id mediumint(6) unsigned NOT NULL auto_increment, upload_date timestamp(14) NOT NULL, title varchar(100) NOT NULL default '', type char(1) NOT NULL default '', filename varchar(100) default NULL, note varchar(100) default NULL, PRIMARY KEY (upload_id)) TYPE=MyISAM;") or die("Error creating uploads table.  Does the web user account have add table permission?");
		$output .= "Added uploads table<br>";
	}



$output .= "</i><p><br>Upgrade completed.  <p>Make sure to read RELEASENOTES.txt for information about configuring new features.";
$p->DisplayPage($output);

?>
